package com.apache.info.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.tools.StrUtil;

public class PubSortInfoManagerImpl implements PubSortInfoManager {

	protected IDao pubSortInfoDao;
	protected IDao pubMetadataDao;
	protected final String entityName = "com.apache.info.entity.PubSortInfo";
	private Map<String, String> metadatas;

	public void setPubSortInfoDao(IDao pubSortInfoDao) {
		this.pubSortInfoDao = pubSortInfoDao;
	}

	public void setPubMetadataDao(IDao pubMetadataDao) {
		this.pubMetadataDao = pubMetadataDao;
	}

	public void setMetadatas(Map<String, String> metadatas) {
		this.metadatas = metadatas;
	}

	public String saveInfo(ParamsVo<PubSortInfo> vo) throws BusinessException {
		PubSortInfo pubSortInfo = vo.getObj();
		String infoId = pubSortInfo.getSortCode();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "pubSortInfo_" + infoId;
		pubSortInfo.setSortId(infoId);
		MethodParam param = new MethodParam("PubSortInfo", cacheKey, "", entityName);
		param.setVaule(pubSortInfo);
		if (pubSortInfoDao.insert(param)) {
			if (!saveMetadatas(pubSortInfo, Validator.getDefaultStr(String.valueOf(vo.getParams("mainTab")), "0")))
				throw new BusinessException("创建元素失败");
			return infoId;
		}
		return "";
	}

	/**
	 * description:  自动生成公共元素字段
	 * @param sortId
	 */
	private boolean saveMetadatas(PubSortInfo pubSortInfo, String mainTab) {
		List<PubMetadata> list = new ArrayList<PubMetadata>();
		boolean mark = false;

		if (null == metadatas && Validator.isEmpty(metadatas)) {
			return true;
		}
		StringBuffer attrs = new StringBuffer();
		String primaryKey = "";
		if ("1".equals(mainTab)) {
			for (String key : metadatas.keySet()) {
				PubMetadata m1 = new PubMetadata();
				m1.setMetadataId(Validator.generate());
				m1.setSortId(pubSortInfo.getSortId());
				String str[] = metadatas.get(key).split(",");
				m1.setDataFullName(str[0]);
				m1.setDataAttr(key);
				m1.setDataShortName(str[1]);
				m1.setDataLength(Long.valueOf(str[2]));
				m1.setDataRestrict(str[3]);
				m1.setDataType(str[4]);
				m1.setIfAdd("F");
				m1.setIfEdit("F");
				m1.setIfList("F");
				m1.setIfSearch("F");
				attrs.append("," + key);
				if ("1".equals(str[3])) {
					primaryKey = str[1];
				}
				list.add(m1);
			}
			PubMetadata m1 = new PubMetadata();
			m1.setMetadataId(Validator.generate());
			m1.setSortId(pubSortInfo.getSortId());
			m1.setDataFullName("子表分类代号");
			m1.setDataAttr("subSortId");
			m1.setDataShortName("sub_sort_id");
			m1.setDataLength(36l);
			m1.setDataRestrict("3");
			m1.setDataType("5");
			m1.setIfAdd("F");
			m1.setIfEdit("F");
			m1.setIfList("F");
			m1.setIfSearch("F");
			attrs.append(",subSortId");
			list.add(m1);
		} else {
			PubMetadata m1 = new PubMetadata();
			m1.setMetadataId(Validator.generate());
			m1.setSortId(pubSortInfo.getSortId());
			m1.setDataFullName("主键");
			m1.setDataAttr("subId");
			m1.setDataShortName("sub_id");
			m1.setDataLength(36l);
			m1.setDataRestrict("1");
			m1.setDataType("5");
			attrs.append(",subId");
			primaryKey = "sub_id";
			list.add(m1);
		}
		MethodParam param = new MethodParam("PubMetadata", "", "", PubMetadata.class.getName());
		param.setVaule(list);
		mark = pubMetadataDao.insertBath(param);
		String cacheKey = pubSortInfo.getSortCode() + "_tableAttr";
		String cacheContent = cacheKey + ";" + attrs.substring(1).toString() + ";" + primaryKey + ";"
				+ pubSortInfo.getInfoTableName();
		SystemTools.getInstance().getCache("").createCacheObject(cacheKey, cacheContent);
		return mark;
	}

	public boolean editInfo(ParamsVo<PubSortInfo> vo) throws BusinessException {
		PubSortInfo pubSortInfo = vo.getObj();
		if (Validator.isNotNull(pubSortInfo.getSortId())) {
			String cacheKey = "pubSortInfo_" + pubSortInfo.getSortId();
			MethodParam param = new MethodParam("PubSortInfo", cacheKey, "", entityName);
			param.setVaule(pubSortInfo);
			return pubSortInfoDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<PubSortInfo> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "pubSortInfo_" + infoId;
		//String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setParams("sortId", infoId);
		param.setDelete(true);
		return pubSortInfoDao.delete(param);
	}

	public Object getInfoById(ParamsVo<PubSortInfo> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "pubSortInfo_" + infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return pubSortInfoDao.selectById(param);
	}

	public Object execute(ParamsVo<PubSortInfo> vo) {
		return null;
	}

	public Page getPageInfo(ParamsVo<PubSortInfo> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = pubSortInfoDao.pageSelect(param);
		return page;
	}

	public List<PubSortInfo> getList(ParamsVo<PubSortInfo> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return pubSortInfoDao.select(param);
	}

	public long countInfo(ParamsVo<PubSortInfo> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return pubSortInfoDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<PubSortInfo> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		PubSortInfo pubSortInfo = vo.getObj();
		String ids = String.valueOf(vo.getParams("sortIds"));
		if (Validator.isNotNull(ids)) {
			param.setParams("sortIds", StrUtil.strArrayToStr(ids.split(","), ",", "'"));
		}
		if (Validator.isEmpty(pubSortInfo))
			return param;
		if (Validator.isNotNull(pubSortInfo.getSortCode())) {
			param.setParams("sortCode", pubSortInfo.getSortCode());
		}
		if (Validator.isNotNull(pubSortInfo.getSortName())) {
			param.setParams("sortName", pubSortInfo.getSortName());
		}
		if (Validator.isNotNull(pubSortInfo.getInfoTableName())) {
			param.setParams("infoTableName", pubSortInfo.getInfoTableName());
		}
		if (Validator.isNotNull(pubSortInfo.getSortStatus())) {
			param.setParams("sortStatus", pubSortInfo.getSortStatus());
		}
		if (Validator.isNotNull(pubSortInfo.getSortDisable())) {
			param.setParams("sortDisable", pubSortInfo.getSortDisable());
		}
		if (Validator.isNotNull(pubSortInfo.getReserved1())) {
			param.setParams("reserved1", pubSortInfo.getReserved1());
		}
		if (Validator.isNotNull(pubSortInfo.getReserved2())) {
			param.setParams("reserved2", pubSortInfo.getReserved2());
		}
		return param;
	}
}
